﻿@using CQIE.TES.Model
@using CQIE.TES.WEB.USER.Models

<select name="chooseEva">
    <option value="">请选择评教</option>
    @{
        foreach (EvaluaHeight item in ViewBag.EvaList1)
        {

            <option value="@item.ID">@item.Name</option>

        }
    }
</select>
<select name="chooseTeachers">
    <option value="">请选择评教教师</option>
    @{
        foreach (Teacher item in ViewBag.teachersList)
        {
            <option value="@item.ID">@item.Name</option>

        }
    }
</select>
<button type="button"
        class="layui-btn layui-btn-normal" onclick="TeachersMakeSure()">
    确定
</button>
<form id="myForm1" class="layui-form" lay-filter="myForm1" style="display: none;">
    @{
        foreach (Question_Option item in ViewBag.newData)
        {
            <div class="layui-form-item">
                <div class="question">
                    @item.Info
                </div>
                <div>
                    <ul class="que" id="que">
                       
                        @foreach (Option o in item.Options)
                        {
                            <li class="frm">
                                <input type="radio" name="@(item.ID)" value="@o.Score" title="@o.Content">
                            </li>

                        }
                    </ul>
                </div>
            </div>
        }
    }
    <Button lay-submit lay-filter="myformSub1" class="layui-btn layui-btn-normal btn1">提交</Button>
</form>

<script>
        //确认函数
        var TeachersMakeSure;
        //登录教师唯一标识符
        //注意：选项卡 依赖 element 模块，否则无法进行功能性操作
        layui.use(['element', 'layer', 'table', 'jquery', 'form'], function () {
            var element = layui.element;
            var layer = layui.layer;
            var table = layui.table;
            var $ = layui.jquery;
            var form = layui.form;

            //确定按钮
            TeachersMakeSure = function () {
                
                //评教evaId
                let evaId = $("[name='chooseEva']").val();
                //目标教师aimId(选择的目标老师的Id)
                let aimId = $("[name='chooseTeachers']").val();
                
                //验证为空
                if (evaId == "") {
                    layer.msg('请选择评教', { icon: 2 });
                    return;
                }
                if (aimId == "") {
                    layer.msg('请选择互评教师', { icon: 2 })
                    return;
                }
                $('#myForm1').css('display', 'block');
                //首先清空所有选择
                $('#myForm1')[0].reset();
                var index = layer.load();
                //将选择的select选项保存到sessionStorage中
                sessionStorage.setItem("evaId", evaId);
                sessionStorage.setItem("aimId", aimId);
                $.get("/api/eva/teacherotherisrecorded",
                    {
                        evaId: sessionStorage.getItem("evaId"),
                        teacherId: sessionStorage.getItem('tId'),
                        aimTeacherId: sessionStorage.getItem('aimId')

                    },
                    function(res) {
                        if (res != null) {
                            //完成评教任务
                            console.log(res);
                            //转换obj
                            let obj = JSON.parse(res);
                            console.log(obj);
                            let data = {}
                            for (let i = 0; i < obj.length; i++) {
                                data[obj[i].qid] = obj[i].aid;
                            }
                            console.log(data);
                            form.val("myForm1", data);
                            $('.btn1').css('display', 'none');
                            form.render();
                        } else {
                            //未完成评教
                            $('.btn1').css('display', 'block');
                            // 清空表单
                            $('#myForm1')[0].reset();
                        }
                        layer.close(index);
                    });

            }

            //提交
            form.on("submit(myformSub1)",
                function(obj) {

                    var data3 = form.val("myForm1");
                    console.log(data3);
                    //判断是否题目全部选择了
                    if (Object.keys(data3).length !== @ViewBag.Count1) {
                        layer.msg('请完成所有题目', { icon: 2 });
                        return false;
                    }
                    //提交加载
                    var index1 = layer.load();

                    $.post("/api/eva/teacherotherisrecorded/submit",
                        {
                            evaId: sessionStorage.getItem("evaId"),
                            teacherId: sessionStorage.getItem('tId'),
                            aimTeacherId: sessionStorage.getItem("aimId"),
                            answerIds: Object.values(data3),
                            questionIds: Object.keys(data3)
                        },
                        function(obj) {
                            //关闭加载

                            if (obj) {
                                layer.msg("提交成功！", { icon: 1 });
                                $.get("/api/eva/teacherotherisrecorded",
                                    {
                                        evaId: sessionStorage.getItem("evaId"),
                                        teacherId: sessionStorage.getItem('tId'),
                                        aimTeacherId: sessionStorage.getItem('aimId')

                                    },
                                    function(res) {
                                        if (res != null) {
                                            //完成评教任务
                                            console.log(res);
                                            //转换obj
                                            let obj = JSON.parse(res);
                                            console.log(obj);
                                            let data = {}
                                            for (let i = 0; i < obj.length; i++) {
                                                data[obj[i].qid] = obj[i].aid;
                                            }
                                            console.log(data);
                                            form.val("myForm1", data);
                                            $('.btn1').css('display', 'none');
                                            form.render();
                                        } else {
                                            //未完成评教
                                            $('.btn1').css('display', 'block');
                                            // 清空表单
                                            $('#myForm1')[0].reset();
                                        }
                                        layer.close(index1);
                                    });
                            } else {
                                layer.msg("失败了~好像出了一些问题！", { icon: 2 });
                            }
                            document.body.scrollTop = document.documentElement.scrollTop = 0;
                        });
                    return false;
                });
        });

</script>